package com.my.chapter15;


import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

public class practice40 {
    public static void main(String[] args)  throws Exception{
        List<SPet> pets = (List<SPet>) Arrays.asList(new SDog(),new SCat(),new SRat(),new SPug(),new SMutt(),new SMouse(),
                new SManx(),new SHamster());
        Apply.apply(pets, SPet.class.getMethod("speak"));
    }
}

class SPet  {
    public void speak() {
        System.out.println(this + " speak");
    }

    @Override
    public String toString() {
        return this.getClass().getSimpleName();
    }
}

class SRodent extends SPet {}

class SDog extends SPet {}

class SCat extends SPet {}

class SRat extends SRodent {}

class SPug extends SDog {}

class SMutt extends SDog {}

class SMouse extends SRodent {}

class SManx extends SCat {}

class SHamster extends SRodent {}

class Apply{
    public static <T,S extends Iterable<? extends T>>
        void apply(S seq, Method f,Object... args){
        try {
            for(T t:seq){
                f.invoke(t,args);
            }
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
}

